﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Reconciliation.PackingAndUnpacking
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Request.QueryString["lang"] != null)
                Session["Lang"] = Request.QueryString["lang"].ToString();
            else if (Session["Lang"] == null)
                Session["Lang"] = "en-US";

            BasePage.SetLocalCulture();
        }

        protected void Login_Click(object sender, EventArgs e)
        {
            Domain.User user = new Domain.User();
            user = Business.User.GetUserByUserNameAndPassword(txtUserID.Text, txtPass.Text);
            if (user != null)
            {
                //FormsAuthentication.RedirectFromLoginPage(txtUserID.Text, false);

                // get the role now

                string userName = user.UserName;

                Session["UserID"] = user.UserID;

              //  bool rememberUserName = chkRemenberMe.Checked;

                Domain.Role role = user.Role;

                string roles = "";

                if (user.Role != null)
                {
                    roles = role.RoleCode.ToString();
                    roles += "," + role.Permission.PermissionCode.ToString();
                }
                /** Roles 
                1	Employee
                2	Administrator
                3	Auditor
                4	Client
                **/                
                // Create forms authentication ticket

                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(

                1, // Ticket version

                userName,// Username to be associated with this ticket

                DateTime.Now, // Date/time ticket was issued

                DateTime.Now.AddMinutes(30), // Date and time the cookie will expire

                false, // if user has chcked rememebr me then create persistent cookie

                roles, // store the user data, in this case roles of the user

                FormsAuthentication.FormsCookiePath); // Cookie path specified in the web.config file in <Forms> tag if any.

                // To give more security it is suggested to hash it

                string hashCookies = FormsAuthentication.Encrypt(ticket);


                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies); // Hashed ticket

                // Add the cookie to the response, user browser

                Response.Cookies.Add(cookie);

                // Get the requested page from the url

                //string returnUrl = Request.QueryString["ReturnUrl"];

                string returnUrl = FormsAuthentication.GetRedirectUrl(userName, false);

                // check if it exists, if not then redirect to default page

                if (returnUrl == null) returnUrl = "~/PackingAndUnpacking.aspx";

                Response.Redirect(returnUrl);

            }
            else
            {
                lblResults.Text = Resources.Lang.MsgInvalidUserPass.ToString();
            }
        }

    }
}